package com.cup.wx.micro.web;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.cup.common.response.ResponseData;
import com.cup.wx.micro.bean.WxAccessToken;
import com.cup.wx.micro.bean.WxUserInfo;
import com.cup.wx.micro.service.WXBaseService;
import com.cup.wx.micro.web.request.WxOfficalRequset;

/**
 * Applet get user base info controller 
 * @author alan.deyu.zhao
 */
@RestController
@RequestMapping("/wxplat")
public class WxOfficialPlatApi {
	
	@Autowired
	private WXBaseService wechatbaseService;
	
	/**
	 * @throws Exception 
	 */
	@GetMapping("authorization")
	private String login(@RequestParam String code) throws Exception{
		ResponseData<Map<String, Object>> body = new ResponseData<>();
		Map<String, Object> rs = wechatbaseService.getAuthorizationAccessToken(code);
		body.init(rs);
		return body.toString();
	}
	
	@GetMapping("test")
	private String test(@RequestParam String code) throws Exception{
		ResponseData<WxAccessToken> body = new ResponseData<>();
		WxAccessToken rs = wechatbaseService.getRedisAcessToken();
		body.init(rs);
		return body.toString();
	}
	
	/**
	 * applet user encrypte userinfo 
	 */
	@PostMapping("info")
	private String info(@RequestBody WxOfficalRequset wxrquest){
		
		ResponseData<WxUserInfo> body = new ResponseData<>();
		
		WxUserInfo wxinfo = wechatbaseService.getWxUserInfo(wxrquest.getAccessToken(), wxrquest.getOpenId());
		
		body.init(wxinfo);
		return body.toString();
	}
}
